import 'package:flutter/material.dart';

class PpTableElement extends StatefulWidget {
  // 传入参数
  const PpTableElement({
    Key? key,
    required this.checkName,
    required this.checkSite,
    required this.checkNfc,
    required this.checkTime,
    required this.checkStartTime,
    required this.checkEndTime,
    required this.result,
    required this.onTap,
    required this.reportContent,
    required this.templateId,
    required this.tag,
  }) : super(key: key,);
  final String checkName;
  final String checkSite;
  final String checkNfc;
  final String checkTime;
  final String checkStartTime;
  final String checkEndTime;
  final String result;
  final VoidCallback onTap;
  final List reportContent;
  final String templateId;
  final String tag;

  @override
  State<StatefulWidget> createState() => PpTableElementState();
}

class PpTableElementState extends State<PpTableElement> {
  List<String> choices = ['OK','NG'];
  Map choicesValues = {'OK':'合格','NG':'不合格'};
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){widget.onTap();},
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: <Widget>[
          Container(
            padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
            child: Container(
              padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
              decoration: BoxDecoration(
                border: Border(bottom: BorderSide(width: 1,color: Colors.pinkAccent)),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 5,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: '开始时间 ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 9,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: widget.tag.toString()==' 未巡查 '?widget.checkStartTime:widget.checkStartTime
                                      ,style: widget.tag.toString()==' 未巡查 '?TextStyle(fontSize:13,color: Colors.redAccent):TextStyle(fontSize:13,color: Colors.blueAccent),),
                                  ]
                              )
                          ),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                  ),
                  SizedBox(height: 10.0,),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 5,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: '结束时间 ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 9,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: widget.tag.toString()==' 未巡查 '?widget.checkEndTime:widget.checkEndTime
                                      ,style: widget.tag.toString()==' 未巡查 '?TextStyle(fontSize:13,color: Colors.redAccent):TextStyle(fontSize:13,color: Colors.blueAccent),),
                                  ]
                              )
                          ),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                  ),
                  SizedBox(height: 10.0,),
                ] + _reportChoice(widget.reportContent) + [
                  //下面三个锁死
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 3,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: '检查人员 ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 2,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: widget.checkName=='null'?'暂无':widget.checkName,style: TextStyle(fontSize:13,color: Colors.black87),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 4,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: '所属位置 ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text:widget.checkSite,style: TextStyle(fontSize:13,color: Colors.pinkAccent),),
                                  ]
                              )
                          ),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                  ),
                  SizedBox(height: 10.0,),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 3,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: '检查结果 ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 2,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: choices.contains(widget.result.toString()) ?choicesValues[widget.result.toString()]:'暂无',
                                      style: widget.result.toString()=='NG'?TextStyle(
                                          fontSize:13,
                                          backgroundColor: Colors.redAccent,
                                          color: Colors.yellow
                                      ):TextStyle(
                                          fontSize:13,
                                          color: Colors.black87
                                      ),
                                    ),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 4,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: 'nfc ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 5,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: ' ' + widget.checkNfc +' ',style: TextStyle(fontSize:13,color: Colors.black87),),
                                  ]
                              )
                          ),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                  ),
                  SizedBox(height: 10.0,),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 4,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: '检查时间 ', style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                                  ]
                              )
                          ),
                        ),
                        Expanded(
                          flex: 15,
                          child:Text.rich(
                              TextSpan(
                                  children: [
                                    TextSpan( text: widget.checkTime=='null'?'暂无':widget.checkTime,style: TextStyle(fontSize:13,color: Colors.black87),),
                                  ]
                              )
                          ),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
                  ),
                  SizedBox(height: 10.0,),
                ],
              ),
            ),
          ),
          Positioned(
              top: 0,
              right: 5,
              child: Container(
                padding: EdgeInsets.fromLTRB(4, 2, 4, 2),
                decoration: BoxDecoration(
                  color: widget.tag== ' 未巡查 '?Colors.redAccent:Colors.lightBlueAccent ,
                  borderRadius: BorderRadius.circular(0),
                  // border: new Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)) ),
                ),
                child: Text(widget.tag,
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.w600,fontSize: 15),
                ),
              )
          ),
        ],
      ),
    );
  }
//  展示不同模板的报表结果
  _reportChoice(List data){
    List<Widget> theList = [];
    for(var item in data){
      theList.add(
        Container(
          child: Row(
            children: [
              Expanded(
                flex: 5,
                child:Text.rich(
                    TextSpan(
                        children: [
                          TextSpan( text: item['label'] , style: TextStyle(fontSize:10,color: Colors.black38,fontWeight: FontWeight.w500),),
                        ]
                    )
                ),
              ),
              Expanded(
                flex: 9,
                child:Text.rich(
                    TextSpan(
                        children: [
                          TextSpan(
                            text: _showResult(item['result']),
                            style: item['result'].toString()=='NG'?TextStyle(
                                fontSize:13,
                                backgroundColor: Colors.redAccent,
                                color: Colors.yellow
                            ):TextStyle(
                                fontSize:13,
                                color: Colors.black87
                            ),
                          ),
                        ]
                    )
                ),
              ),
              SizedBox(height: 20.0,),
            ],
          ),
          padding: EdgeInsets.fromLTRB(2, 2, 2, 2),
        ),
      );
    }
    return theList;
  }
  // 展示不同数据的rsult
  _showResult(String result){
    if(result.toString()==''){
      return result='暂无';
    }
    else if(result.toString()=='NG'){
      return choicesValues[result.toString()];
    }
    else if(result.toString()=='OK'){
      return choicesValues[result.toString()];
    }
    else{
      return result.toString();
    }
  }
}